package jxc.servlet.info.provider;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxc.base.helper.IdGenerator;
import jxc.data.hibernate.dao.ProviderDAO;
import jxc.data.hibernate.pojo.Provider;

/**
 * Servlet implementation class Update
 */
@WebServlet(name = "UpdateProvider", urlPatterns = { "/info/provider/update" })
public class Update extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * 返回更新界面
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		Provider provider=null;
		if(id!=null&&id.length()>0){
			ProviderDAO pDao=new ProviderDAO();
			provider=pDao.getById(id);
		}else{
			provider=new Provider();
			provider.setId(IdGenerator.getProviderId());
		}
		request.setAttribute("p", provider);
		request.getRequestDispatcher("/info/provider/update.jsp").forward(request, response);
	}

	/**
	 * 更新数据
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		String name=request.getParameter("name");
		String zip=request.getParameter("zip");
		String address=request.getParameter("address");
		String tel=request.getParameter("tel");
		String linkman=request.getParameter("linkman");
		String phone=request.getParameter("phone");
		String bank=request.getParameter("bank");
		String account=request.getParameter("account");
		String email=request.getParameter("email");
		String fax=request.getParameter("fax");
		
		ProviderDAO pDao=new ProviderDAO();
		Provider provider=new Provider(id, name, zip, address, tel, linkman, phone, bank, account, email, fax, 1, null, null, null);
		pDao.saveOrUpdate(provider);
		pDao.commit();
		request.setAttribute("message", "成功更新供货商信息");
		this.doGet(request, response);
	}

}
